package com.neusoft.neuiotms.dcam.fixed.service.spareconsume.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.neusoft.bizcore.activiti.model.WorkOrder;
import com.neusoft.bizcore.activiti.repository.WorkOrderRepository;
import com.neusoft.bizcore.activiti.service.ProcessInstanceService;
import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.support.Searchable;
import com.neusoft.neuiotms.dcam.constant.DCAMConsts;
import com.neusoft.neuiotms.dcam.fixed.converter.spareconsume.SpareConsumeStockConver;
import com.neusoft.neuiotms.dcam.fixed.dto.StockDTO;
import com.neusoft.neuiotms.dcam.fixed.dto.spareconsume.SpareConsumeStockDTO;
import com.neusoft.neuiotms.dcam.fixed.model.spareconsume.SpareConsumeApply;
import com.neusoft.neuiotms.dcam.fixed.model.spareconsume.SpareConsumeStock;
import com.neusoft.neuiotms.dcam.fixed.repository.spareConsume.SpareConsumeApplyRepository;
import com.neusoft.neuiotms.dcam.fixed.repository.spareConsume.SpareConsumeStockRepository;
import com.neusoft.neuiotms.dcam.fixed.service.spareconsume.SpareConsumeStockService;

@Service
public class SpareConsumeStockServiceImpl implements SpareConsumeStockService {

    @Autowired
    private ProcessInstanceService processInstanceService;
    @Autowired
    private SpareConsumeStockRepository spareConsumeStockRepository;
    @Autowired
    private SpareConsumeStockConver spareConsumeStockConver;
    @Autowired
    private WorkOrderRepository workOrderRepository;
    @Autowired
    private SpareConsumeApplyRepository spareConsumeApplyRepository;

    @Override
    public List<SpareConsumeStockDTO> create(final List<SpareConsumeStockDTO> dtos, final UserBean user) {
        // 完成节点
        if ((dtos != null) && (dtos.size() > 0)) {
            final SpareConsumeStockDTO dto = dtos.get(0);
            this.processInstanceService.complete(dto.getWorkOrderNum(), dto.getTaskId(), user);
            final List<SpareConsumeStock> lstStocks =
                    this.spareConsumeStockRepository.findByWorkNumAndTaskId(dto.getWorkOrderNum(),
                            dto.getTaskId());
            lstStocks.forEach(r -> {
                if (r.getStock() != null) {
                    r.getStock().setStatus(DCAMConsts.STOCK_STATUS_NEW);
                    r.getStock().setUpdateTime(new Date());
                }
            });
            this.spareConsumeStockRepository.deleteInBatch(lstStocks);
            lstStocks.clear();
            dtos.forEach(r -> {
                final SpareConsumeStock spareConsumeStock = this.spareConsumeStockConver.toModel(r);
                if (spareConsumeStock.getStock() != null) {
                    spareConsumeStock.getStock().setStatus(DCAMConsts.STOCK_STATUS_LOCK);
                    spareConsumeStock.getStock().setUpdateTime(new Date());
                }
                lstStocks.add(spareConsumeStock);

            });
            this.spareConsumeStockRepository.saveAll(lstStocks);
            dtos.clear();
            lstStocks.forEach(r -> {
                dtos.add(this.spareConsumeStockConver.toDTO(r));
            });
        }
        return dtos;
    }

    @Override
    public List<SpareConsumeStockDTO> detail(final String workNum, final String taskId, final UserBean user) {
        return null;
    }

    @Override
    public Page<SpareConsumeStockDTO> search(final Pageable pageable, final Searchable searchable) {
        final Page<SpareConsumeStock> result = this.spareConsumeStockRepository.search(pageable, searchable);
        return this.spareConsumeStockConver.toPageDTO(result);
    }

    @Override
    public List<StockDTO> finishList(final String workNum) {
        final List<StockDTO> dtos = new ArrayList<>();
        // 查询故障工单的ID
        final WorkOrder workOrder = this.workOrderRepository.findByWorkOrderNum(workNum);
        if (null != workOrder) {
            final Long id = workOrder.getId();
            // 查询关联的备件工单
            final List<SpareConsumeApply> spareConsumes = this.spareConsumeApplyRepository.findByMalfunction(id);

            if (null != spareConsumes) {
                final List<String> workNums = spareConsumes.stream().map(s -> s.getWorkNum()).filter(l -> null != l)
                        .collect(Collectors.toList());
                if (workNums.size() > 0) {
                    final List<SpareConsumeStock> stocks =
                            this.spareConsumeStockRepository.findByStock_StatusAndWorkNumIn("used", workNums);
                    if (stocks != null) {
                        final List<SpareConsumeStockDTO> results = this.spareConsumeStockConver.toListDTO(stocks);
                        results.stream().map(s -> s.getStock()).forEach(dtos::add);
                    }
                }
            }
        }

        return dtos;
    }

    @Override
    public List<StockDTO> finishList(String workNum, String confirmUser) {
        final List<StockDTO> dtos = new ArrayList<>();
        // 查询故障工单的ID
        final WorkOrder workOrder = this.workOrderRepository.findByWorkOrderNum(workNum);
        if (null != workOrder) {
            final Long id = workOrder.getId();
            // 查询关联的备件工单
            final List<SpareConsumeApply> spareConsumes = this.spareConsumeApplyRepository.findByMalfunction(id);

            if (null != spareConsumes) {
                final List<String> workNums =
                        spareConsumes.stream().filter(it -> confirmUser.equals(it.getConfirmUser()))
                                .map(s -> s.getWorkNum()).filter(l -> null != l)
                                .collect(Collectors.toList());
                if (workNums.size() > 0) {
                    final List<SpareConsumeStock> stocks =
                            this.spareConsumeStockRepository.findByStock_StatusAndWorkNumIn("used", workNums);
                    if (stocks != null) {
                        final List<SpareConsumeStockDTO> results = this.spareConsumeStockConver.toListDTO(stocks);
                        results.stream().map(s -> s.getStock()).forEach(dtos::add);
                    }
                }
            }
        }

        return dtos;
    }
}
